package com.oocl.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oocl.dao.MuserDao;
import com.oocl.dao.impl.MuserDaoImpl;
import com.oocl.dto.MUserDTO;
import com.oocl.exception.MyException;
import com.oocl.pojo.Muser;
import com.oocl.service.MuserService;
import com.oocl.util.Base64Util;
import com.oocl.util.PropertiesFactory;


/*
 * @author Garen chen
 * @description : merchant user service
 * @date 2017-07-28
 * */
public class MuserServiceImpl implements MuserService{
      private MuserDao mDao;
      
      
	
	public MuserServiceImpl() {
		this.mDao = new MuserDaoImpl();
	}

	/*
	 * @method : addMuser
	 * @function : add one Muser
	 * @param  m :Muser 
	 * @return  Muser id
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public String addMuser(Muser m) throws MyException {
		System.out.println("MuserServiceImpl:"+m.getToken());
		String id = null;
		 try {
			id = mDao.addMuser(m);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MyException("添加出错",e.getClass().getName());
		}
		return id;
	}
   
	 
	/*
	 * @method : findAll
	 * @function : load all Muser
	 * @param 
	 * @return  Muser List
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public List<Muser> findAll() throws MyException {
		List<Muser> list = null;
		try {
			list = mDao.findAll();
		} catch (SQLException e) {
			throw new MyException("查询出错",e.getClass().getName());
		}
		return list;
	}
	
	/*
	 * @method : loadMuser
	 * @function : load a Muser
	 * @param  m Muser
	 * @return  muser Muser
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public Muser loadMuser(Muser m) throws MyException {
		Muser muser = null;
		 try {
			 muser = mDao.loadMuser(m);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MyException("查询出错",e.getClass().getName());
		}
		return muser;
	}
	
	/*
	 * @method : loadMuser
	 * @function : load a Muser
	 * @param  mid Muser id
	 * @return  muser Muser
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public Muser loadMuserById(String mid) throws MyException {
		Muser muser = null;
		 try {
			 muser = mDao.loadMuserById(mid);
			 String fileName = muser.getPortrait();
				InputStream in = new FileInputStream(PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("portraitfile")+File.separator+fileName);
				byte[] bs = new byte[in.available()];
				in.read(bs);
				in.close();
				String picContent = Base64Util.wrapBase64forjsp(bs);
				
				muser.setPortrait(picContent);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException e) {
			throw new MyException("查询出错",e.getClass().getName());
		}
		return muser;
	}

	
	/*
	 * @method : loadMuserDTOById
	 * @function : load a MUserDTO
	 * @param  mid Muser id
	 * @return  md MUserDTO
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public MUserDTO loadMuserDTOById(String mid)
			throws MyException {
		MUserDTO md = null;
		 try {
			  Muser muser = mDao.loadMuserById(mid);
			  String picContent=null;
			  String date = null;
			  if(null!=muser.getPortrait()){
				  
				  String fileName = muser.getPortrait();
				  InputStream in = new FileInputStream(PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("foodpath")+PropertiesFactory.getPro().getProperty("foodpath")+File.separator+fileName);
				  byte[] bs = new byte[in.available()];
				  in.read(bs);
				  in.close();
				  picContent = Base64Util.wrapBase64forjsp(bs);
			  }
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				if(null!=muser.getCheckedDate()){
					date = df.format(muser.getCheckedDate());
				}
				md= new MUserDTO(muser.getIcnum(), muser.getMname(),picContent, muser.getMaddr(),date);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException e) {
			throw new MyException("查询出错",e.getClass().getName());
		}
		return md;
	}
	
	
	/*
	 * @method : loadMuserDTOByIcnum
	 * @function : load a MUserDTO
	 * @param  mid Muser icnum
	 * @return  md MUserDTO
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public MUserDTO loadMuserDTOByIcnum(String icnum)
			throws MyException {
		MUserDTO md = null;
		 try {
			 System.out.println("icnum length:---"+icnum.length());
			  Muser muser = mDao.loadMuserByIcnum(icnum);
			  String picContent=null;
			  String date = null;
			  if(null!=muser.getPortrait()){
				  String fileName = muser.getPortrait();
				  InputStream in = new FileInputStream(PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("portraitfile")+File.separator+fileName);
				  byte[] bs = new byte[in.available()];
				  in.read(bs);
				  in.close();
				  picContent = Base64Util.wrapBase64forjsp(bs);
			  }
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				if(null!=muser.getCheckedDate()){
					date = df.format(muser.getCheckedDate());
				}
				md= new MUserDTO(muser.getIcnum(), muser.getMname(),picContent, muser.getMaddr(),date);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException e) {
			throw new MyException("查询出错",e.getClass().getName());
		}
		return md;
	}

	
	/*
	 * @method : loadMuserByIcnum
	 * @function : load a Muser
	 * @param  icnum: Muser icnum
	 * @return  md Muser
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public Muser loadMuserByIcnum(String icnum) throws MyException {
		Muser muser = null;
		try {
			muser = mDao.loadMuserByIcnum(icnum);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MyException("查询出错",e.getClass().getName());
		}
		return muser;
	}

	
	/*
	 * @method : updateMuserStatus1
	 * @function : update a Muser status1
	 * @param  muser: Muser 
	 * @return  update  number
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public int updateMuserStatus1(Muser muser) throws MyException {
		int count =-1;
		
		try {
			count = mDao.updateMuserStatus1(muser);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MyException("更新出错",e.getClass().getName());
		}
		
		
		return count;
	}

	
	/*
	 * @method : updateMuser
	 * @function : update a Muser 
	 * @param  muser: Muser 
	 * @return  update  number
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public int updateMuser(Muser omuser) throws MyException {
int count =-1;
		
		try {
			count = mDao.updateMuser(omuser);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MyException("更新出错",e.getClass().getName());
		}
		
		
		return count;
	}

	
	/*
	 * @method : loadMusersDTOsByFtype
	 * @function : get muusers by foodType
	 * @param  ftype: foodType 
	 * @return  List<MUserDTO>
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public List<MUserDTO> loadMusersDTOsByFtype(String ftype)
			throws MyException {
		List<MUserDTO> list = new ArrayList<MUserDTO>();
		
		try {
			List<Muser> mlist = mDao.loadMusersDTOsByFtype(ftype);
			for(Muser muser:mlist){
				 String picContent=null;
				  String date = null;
				  if(null!=muser.getPortrait()){
					  String fileName = muser.getPortrait();
					  InputStream in;
						in = new FileInputStream(PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("portraitfile")+File.separator+fileName);
					 
					  byte[] bs = new byte[in.available()];
					  in.read(bs);
					  in.close();
					  picContent = Base64Util.wrapBase64forjsp(bs);
				  }
					DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
					if(null!=muser.getCheckedDate()){
						date = df.format(muser.getCheckedDate());
					}
					MUserDTO md= new MUserDTO(muser.getIcnum(), muser.getMname(),picContent, muser.getMaddr(),date);
					list.add(md);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

}
